package com.lilidp.service.impl;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.lilidp.dto.Result;
import com.lilidp.entity.ShopType;
import com.lilidp.mapper.ShopTypeMapper;
import com.lilidp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lilidp.utils.RedisConstants;
import io.lettuce.core.RedisURI;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 栗顺
 
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result queryList() {
        String key= RedisConstants.SHOP_GEO_KEY;
        //首先查询redis
        String string = stringRedisTemplate.opsForValue().get(key);
        //如果查询结果不空
        if(StrUtil.isNotBlank(string)){
            //直接返回
            List<ShopType> list = JSONUtil.toBean(string, new TypeReference<List<ShopType>>() {},false);
            return Result.ok(list);
        }
        //查询结果为空，查数据库
        List<ShopType> list = query().orderByAsc("sort").list();
        //回设缓存
        stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(list));
        return Result.ok(list);

    }
}
